package com.etl.dataflow.scheduler.timewheel;

import cn.hutool.core.date.DateUtil;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间表达式
 *
 * @author dx
 */
@Data
public class TimeExpression {
    private Integer year;
    private Integer month;
    private Integer day;
    private Integer hour;
    private Integer minute;
    private Integer second;

    public static TimeExpression of(Date date) {
        LocalDateTime localDateTime = DateUtil.toLocalDateTime(date);
        TimeExpression timeExpression = new TimeExpression();
        timeExpression.setYear(localDateTime.getYear());
        timeExpression.setMonth(localDateTime.getMonthValue());
        timeExpression.setDay(localDateTime.getDayOfMonth());
        timeExpression.setHour(localDateTime.getHour());
        timeExpression.setMinute(localDateTime.getMinute());
        timeExpression.setSecond(localDateTime.getSecond());
        return timeExpression;
    }

    public boolean isCurrentYear() {
        int theYear = Calendar.getInstance().get(Calendar.YEAR);
        return theYear == year;
    }

    public boolean isCurrentMonth() {
        int mon = Calendar.getInstance().get(Calendar.MONTH) + 1;
        return isCurrentYear() && mon == month;
    }

    public boolean isCurrentDay() {
        int dayOfMonth = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        return isCurrentMonth() && dayOfMonth == day;
    }
}
